package com.heima.controller;

import com.heima.pojo.AliyunOSSProperties;
import com.heima.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {

    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile file) throws IOException {
        log.info("用户的名称: {}, 用户的年龄: {}", username, age);

        String originalFilename = file.getOriginalFilename();

        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        file.transferTo(new File("d:/file/" + fileName));

        return Result.success();
    }*/
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/api/vm-service/sku/fileUpload")
    public String upload(MultipartFile fileName) throws Exception {
        log.info("文件上传: {}", fileName.getOriginalFilename());

        String extName = fileName.getOriginalFilename().substring(fileName.getOriginalFilename().lastIndexOf("."));

        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), fileName.getBytes(), extName);

        return url;
    }
}
